var $ = require('jquery');
var config = {
    serverHost: 'http://localhost:8080/'
}

var _util = {
    request: function(param) {
        var _this = this;
        $.ajax({
            type: param.method || "GET",
            url: param.url || "",
            dataType: param.type || "json",
            data: param.data || "",
            success: function(res){
                if(0 === res.status){
                    typeof param.success === 'function' && param.success(res.data, res.msg);
                }
                else if (10 === res.status){
                    window.location.href = './account-login.html?redirect=' + encodeURIComponent(window.location.href);
                }
                else if (1 === res.status){
                    typeof param.error === 'function' && param.error(res.msg);
                }
            },
            error: function(errMsg){
                //todo 请求错误
            }
        })
    },
    doLogin : function(){
        window.location.href = "./account-login.html?redirect="+encodeURIComponent(window.location.href);
    },
    getServerURL: function(path){
        return config.serverHost + path;
    }
}

module.exports = _util;